import { createRouter, createWebHashHistory } from 'vue-router'
const routes = [
    //首页
    {
        path: '/',
        name: 'home',
        component: () => import('../views/home.vue'),


    },
    {
        path: '/home',
        name: 'home',
        component: () => import('../views/home.vue'),
        children: [
            //用户列表
            {
                path: '/users',
                name: 'users',
                component: () => import('../views/users.vue')
            }
        ]
    },
    //登录
    {
        path: '/login',
        name: 'login',
        component: () => import('../views/login.vue')
    }
]
const router = createRouter({
    history: createWebHashHistory(),
    routes
})
//导航首位
router.beforeEach((to, from, next) => {
    const userinfo = JSON.parse(localStorage.getItem('userInfo'))
    if (to.path == '/login' || to.path == '/') {
        if (userinfo) {
            next('/home')
        }
        next()
    }
    if (to.path == '/home') {
        if (!userinfo) {
            next('/login')
        }
        next()
    }
    next()
})
export default router